package com.pzr.soccer.crawlerHelper;

import com.pzr.soccer.consts.GeneralConst;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
public class SeleniumHelper  {

    private static ChromeOptions options = new ChromeOptions();
    public static WebDriver chromeDriver;
    private static long interval = 200;
    private static final  String chromeDriverLocation="c:/chromeDriver/chromedriver.exe";
    private  static final String chromeBinaryLocation="C:/Program Files/Google/Chrome/Application/chrome.exe";
    private  static final String chromeBinaryLocation_other="c:/Program Files (x86)/Google/Chrome/Application/chrome.exe";

    private static void setOptions() {
        System.out.println("start init driver:..........");

        System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
        String osName = System.getProperty("os.name");
        if (!osName.equals("Windows 10")) {
            try {
                options.setBinary(chromeBinaryLocation);
            } catch (Exception e) {
                options.
                        setBinary(chromeBinaryLocation_other);
                System.out.println("no such binary!");
            }
        }


        // 新版Chromedriver设置去除浏览器上的爬虫信息
        options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});

        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("profile.default_content_settings.javascript", 2); // 2就是代表禁止加载的意思
        // 禁止加载css
        prefs.put("profile.default_content_settings.images", 2); // 2就是代表禁止加载的意思
        options.setExperimentalOption("prefs", prefs);

        options.addArguments("disable-infobars"); // 禁止提示
        options.addArguments("--headless"); // 禁止打开窗口
        options.addArguments("disable-popup-blocking");// 禁止弹窗
        options.addArguments("disable-extensions");// 禁止插件
        options.addArguments("--start-maximized");// 窗口最大化

        options.addArguments("disable-application-cache"); // to disable cache
        options.addArguments("safebrowsing-disable-download-protection");
        options.addArguments("ignore-certificate-errors");
        options.addArguments("disable-gpu");
        options.addArguments("incognito");

        // options.setPageLoadStrategy(PageLoadStrategy.NONE); 这种策略和用jsoup差不多的效果

        // 设置代理
        /*
         * String poxyAddress="139.180.178.208:12358"; Proxy proxy=new
         * Proxy().setHttpProxy(poxyAddress).setSslProxy(poxyAddress);
         * options.setProxy(proxy);
         */

    }

    static {
        setOptions();
        chromeDriver = new ChromeDriver(options);
        chromeDriver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
        System.out.println("end init driver");

    }

    public static String getContent(String url) {
         String body="";
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            chromeDriver.get(url);
            body = chromeDriver.getPageSource();
        } catch (Exception e) {
            System.out.println("selenium occur error: " + e.getMessage());
            e.printStackTrace();
            body= GeneralConst.Parse_Errors_Occurred+e.getMessage();
        }

        return body;
    }

}
